package com.vaadin.tutorial.crm.backend.service;

import com.vaadin.tutorial.crm.backend.entity.Student;
import com.vaadin.tutorial.crm.backend.repository.StudentRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.vaadin.tutorial.crm.backend.util.CommonVar.SAVE_NULL;

/**
 * @Description:
 * @Author: Fan
 * @Date: 2021/1/16 20:40
 * @Version 1.0
 */
@Service
public class StudentService {
    private static final Logger LOGGER = Logger.getLogger(StudentService.class.getName());
    private StudentRepository stuRepository;

    public StudentService(StudentRepository stuRepository) {
        this.stuRepository = stuRepository;
    }

    public List<Student> findAll(){
        return stuRepository.findAll();
    }

    public List<Student> findAll(String stringFilter){
        if(stringFilter == null || stringFilter.isEmpty()){
            return stuRepository.findAll();
        }else{
            return stuRepository.search(stringFilter);
        }
    }

    public long count() {
        return stuRepository.count();
    }

    public void delete(Student tstudent) {
        stuRepository.delete(tstudent);
    }

    public void save(Student tstudent) {
        if (tstudent == null) {
            LOGGER.log(Level.SEVERE,
                    SAVE_NULL);
            return;
        }
        stuRepository.save(tstudent);
    }

    public Student getStuListByStuId(Integer stuId){
        return stuRepository.getStuByStuId(stuId);
    }
}
